﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DDD4Fun.Factory
{

    public abstract class BaseFacroty
    {
        public IDictionary<string, object> package
                                   = new Dictionary<string, object>();
  
        public IFruit BuyFruit(string Name, string NameWhatYouWant)
        {
            try
            {
                IFruit MyFruit = CreateFruit(Name);
                MyFruit.name = NameWhatYouWant;
                package.Add(Guid.NewGuid().ToString(), MyFruit);
                return MyFruit;
            }
            catch (TypeLoadException e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public void ShowPackage()
        {
            foreach (KeyValuePair<string, object> kvp in package)
            {
                IFruit f = kvp.Value as IFruit;
                f.SaySomething();
            }
        }

        public abstract IFruit CreateFruit(string Name);
    }

    //工厂的时候由子类决定
    public class FruitFactory : BaseFacroty
    {
        public override IFruit CreateFruit(string Name)
        {
            Type type = Type.GetType("DDD4Fun.Factory." + Name, true);
            IFruit MyFruit = (IFruit)Activator.CreateInstance(type);
            return MyFruit;
        }

    }
}
